{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b26ae530",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0d6d9430",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getCoinMarketCapData(sort_by, sort_type, convert_to, convert_type, aux, limit):\n",
    "    url = f\"https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=0&limit={limit}&sortBy={sort_by}&sortType={sort_type}&convert={convert_to}&cryptoType={convert_type}&aux{aux}\"\n",
    "    response = requests.request(\"GET\", url)\n",
    "    coins = []\n",
    "    for crypto in response.json()['data']['cryptoCurrencyList']:\n",
    "        crypto_data = {\n",
    "            'name': crypto['name'],\n",
    "            'price': f\"{crypto['quotes'][2]['price']:.2f}\", # [0 for BTC / 1 for ETH / 2 for USD]\n",
    "            'change_1d': f\"{crypto['quotes'][2]['percentChange24h']:.2f}\", # [0 for BTC / 1 for ETH / 2 for USD]\n",
    "            'change_7d': f\"{crypto['quotes'][2]['percentChange7d']:.2f}\", # [0 for BTC / 1 for ETH / 2 for USD]\n",
    "            'market_cap': f\"{crypto['quotes'][2]['marketCap']:.0f}\", # [0 for BTC / 1 for ETH / 2 for USD]\n",
    "            'volume_1d': f\"{crypto['quotes'][2]['volume24h']:.0f}\", # [0 for BTC / 1 for ETH / 2 for USD]\n",
    "            'circulating_supply': f\"{crypto['circulatingSupply']:.0f}\"\n",
    "        }\n",
    "        coins.append(crypto_data)\n",
    "    df = pd.DataFrame(coins)\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e9193dc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              name     price change_1d change_7d    market_cap    volume_1d  \\\n",
      "0          Bitcoin  36933.70     -5.12     -7.81  702904193404  39678671607   \n",
      "1         Ethereum   2755.30     -3.09     -6.63  332447411935  21472509083   \n",
      "2           Tether      1.00     -0.01     -0.02   83151888782  80852847471   \n",
      "3              BNB    380.15     -2.98     -6.30   62069907972   2255431854   \n",
      "4         USD Coin      1.00      0.02     -0.02   48734362715   4900073672   \n",
      "...            ...       ...       ...       ...           ...          ...   \n",
      "10121      VIKTAMA      0.00      4.94     21.55             0            0   \n",
      "10122    Astronaut      0.00      0.00      0.00             0            0   \n",
      "10123  Tierra Meta      0.13     -0.47      0.52             0            0   \n",
      "10124      TOKAMAK      0.00      3.68      8.98             0            0   \n",
      "10125      APEBORG      0.00     -3.23    -50.13             0            0   \n",
      "\n",
      "      circulating_supply  \n",
      "0               19031512  \n",
      "1              120657566  \n",
      "2            83152377104  \n",
      "3              163276975  \n",
      "4            48733508388  \n",
      "...                  ...  \n",
      "10121                  0  \n",
      "10122                  0  \n",
      "10123                  0  \n",
      "10124                  0  \n",
      "10125                  0  \n",
      "\n",
      "[10126 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "sort_by = 'market_cap'\n",
    "sort_type = 'desc'\n",
    "convert_to='USD,BTC,ETH'\n",
    "convert_type='all'\n",
    "aux='ath,atl,high24h,low24h,num_market_pairs,cmc_rank,date_added,max_supply,circulating_supply,total_supply,volume_7d,volume_30d,self_reported_circulating_supply,self_reported_market_cap'\n",
    "limit = '20000'\n",
    "df = getCoinMarketCapData(sort_by, sort_type, convert_to, convert_type, aux, limit)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34381e42",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tradesystem1]",
   "language": "python",
   "name": "conda-env-tradesystem1-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
